package com.spring.rechargeapi.web.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.rechargeapi.web.requestModel.ApplicationUpdate;
import com.spring.rechargeapi.web.service.RechargeApiService;
import com.spring.security.core.annotation.OaRestPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.tools.MyTools;
//import com.spring.iosservice.web.service.IosQuestService;
@Controller
@RequestMapping(value = "/rechargeApi")
public class RechargeApiController {
	
	
	@Resource
	private RechargeApiService rechargeApiService;
	

	//充值卡优惠规则
	@RequestMapping(value = "/rechargeRule")
	@ResponseBody
    public Object rechargeRule(HttpServletRequest request,String callback) {
    	Map<String,Object> m  = rechargeApiService.rechargeRule();
        if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
    }
	
	/**
	 * 充值卡申请
	 * @param token
	 * @param recharge_amount
	 * @param user_name
	 * @param identity_card
	 * @param phone
	 * @param apply_way_code
	 * @param callback
	 * @return
	 */
	 
		@RequestMapping(value = "/applyCard")
		@ResponseBody
	    public Object applyCard (String token,String recharge_amount,String user_name,String identity_card,
				String phone,String apply_way_code,String rechargecard_level_code,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.applyCard(token, recharge_amount, user_name, identity_card, phone, apply_way_code, rechargecard_level_code);
		        
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		/**
		 * 查看充值卡申请
		 * @param token
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getApplicationList")
		@ResponseBody
	    public Object getApplicationList (String token,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getApplicationList(token);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
  	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		

		/**
		 * 修改申请
		 * @param token
		 * @param callback
		 * @return
		 */
		//@RequestMapping(value = "/updateApplication")
		@ResponseBody
	    public Object updateApplication (String token,
				String recharge_application_id, String recharge_amount,
				String user_name, String identity_card, String phone,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.updateApplication(token, recharge_application_id, recharge_amount, user_name, identity_card, phone);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		/**
		 * 取消预约充值卡申请
		 * @param token
		 * @param recharge_application_id
		 * @param recharge_amount
		 * @param user_name
		 * @param identity_card
		 * @param phone
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/cancelApplication")
		@ResponseBody
	    public Object cancelApplication (String token,String recharge_application_id, 
	    		String cancel_reason,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.cancelApplication(token, recharge_application_id, cancel_reason);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		/**
		 * 查看自己有的充值卡
		 * @param token
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getCardList")
		@ResponseBody
	    public Object getCardList (String token,String rechargecard_id,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getCardList(token,rechargecard_id);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		/**
		 * 充值卡消费产生订单
		 * @param token
		 * @param rechargecard_id
		 * @param custom_amount
		 * @param business_type_code
		 * @param kids_park_id
		 * @param comment
		 * @param payment_way_code
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/checkOutCreateBill")
		@ResponseBody
	    public Object checkOutCreateBill (String token,String rechargecard_id,String custom_amount,
				String business_type_code,String kids_park_id,String comment,String payment_way_code,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.checkOutCreateBill(token, rechargecard_id, custom_amount, business_type_code, kids_park_id, comment, payment_way_code);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		
		
		@RequestMapping(value = "/checkOut")
		@ResponseBody
	    public Object checkOut (String token, String card_consume_his_id, String rechargecard_id, 
				String amount,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.checkOut(token, card_consume_his_id, rechargecard_id, amount);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		/**
		 * 查看充值卡流水表
		 * @param token
		 * @param rechargecard_id
		 * @param page_size
		 * @param page_index
		 * @param skip
		 * @param rows
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getRechargeBillList")
		@ResponseBody
	    public Object getRechargeBillList (String token,String rechargecard_id,String page_size,
				String page_index,String skip,String rows,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getRechargeBillList(token, rechargecard_id,page_size,
						page_index,skip, rows);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		/**
		 * 查看充值卡充值历史
		 * @param token
		 * @param rechargecard_id
		 * @param page_size
		 * @param page_index
		 * @param skip
		 * @param rows
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getRechargeHistory")
		@ResponseBody
	    public Object getRechargeHistory (String token,String rechargecard_id,String page_size,
				String page_index,String skip,String rows,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getRechargeHistory(token, rechargecard_id, page_size, page_index, skip, rows);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		
		
		/**
		 * 查看本卡有哪些可以购买的套餐
		 * @param token
		 * @param rechargecard_id
		 * @param kids_park_id
		 * @param page_size
		 * @param page_index
		 * @param skip
		 * @param rows
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getPackageList")
		@ResponseBody
	    public Object getPackageList (String token,String rechargecard_id,String kids_park_id,String page_size,
				String page_index,String skip,String rows,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getPackageList(token, kids_park_id, rechargecard_id, page_size, page_index, skip, rows);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		/**
		 * 购买课程套餐
		 * @param token
		 * @param rechargecard_id
		 * @param package_id
		 * @param c_family_member_id
		 * @param comment
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/buyPackage")
		@ResponseBody
	    public Object buyPackage (String token,String rechargecard_id,
				String package_id, String c_family_member_id, String comment,String baby_name,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.buyPackage(token, rechargecard_id, package_id, c_family_member_id, comment,baby_name);
				
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}
	    	
	    	
	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		/**
		 * 查看自己的核心业务
		 * @param token
		 * @param rechargecard_id
		 * @param package_id
		 * @param c_family_member_id
		 * @param comment
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getCardDetailList")
		@ResponseBody
	    public Object getCardDetailList (String token,String rechargecard_id,
				String c_family_member_id,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getCardDetailList(token, rechargecard_id, c_family_member_id);
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		
		/**
		 * 获取充值等级对应最低充值金额
		 * @param callback
		 * @return
		 */
		@RequestMapping(value = "/getApplyConditionList")
		@ResponseBody
	    public Object getApplyConditionList(String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getApplyCondition();
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		
		@RequestMapping(value = "/calculateUserGetAmount")
		@ResponseBody
	    public Object calculateUserGetAmount(String discount_type,String rechargeCardCategory,String recharge_amount,HttpServletRequest request,HttpServletResponse response,String callback) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
			response.setHeader("Access-Control-Allow-Origin", "http://oa.dibabo.cn");
	
			
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.calculateUserGetAmount(recharge_amount,rechargeCardCategory,discount_type);
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		@OaRestPermission(value="/consumeBackground/courseSignIn")
		@RequestMapping(value = "/applyCardBackground")
		@ResponseBody
	    public Object applyCardBackground(@RequestBody Map<String,Object> obj,HttpServletResponse response,String callback,HttpServletRequest request) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
			response.setHeader("Access-Control-Allow-Origin", "*");	
			
			System.out.println("applyCardBackground");
			
			String phone=MyTools.ObjectToString(obj.get("phone"));
			String family_phone=MyTools.ObjectToString(obj.get("family_phone"));
			String address=MyTools.ObjectToString(obj.get("address"));
			String zipcode=MyTools.ObjectToString(obj.get("zipcode"));	

			String recharge_amount=MyTools.ObjectToString(obj.get("recharge_amount"));
			String apply_way_code=MyTools.ObjectToString(obj.get("apply_way_code"));
			String auditor_id=MyTools.ObjectToString(obj.get("auditor_id"));
			String auditor_name=MyTools.ObjectToString(obj.get("auditor_name"));
			String desc=MyTools.ObjectToString(obj.get("desc"));
			String operater_id=MyTools.ObjectToString(obj.get("operator_id"));
			String operater_name=MyTools.ObjectToString(obj.get("operator_name"));
			String consume_recommend_id=MyTools.ObjectToString(obj.get("consume_recommend_id"));
			String consume_recommend_name=MyTools.ObjectToString(obj.get("consume_recommend_name"));
			String kids_parkID=MyTools.ObjectToString(obj.get("kids_park_id"));
			List<Map<String,Object>> babies=(List<Map<String,Object>>)obj.get("babies");
			List<Map<String,Object>> guardians=(List<Map<String,Object>>)obj.get("guardians");
	
			String apply_type=MyTools.ObjectToString(obj.get("apply_type"));

			
			String consume_recommend_deptID=MyTools.ObjectToString(obj.get("consume_recommend_deptID"));
			String consume_recommend_dept_name=MyTools.ObjectToString(obj.get("consume_recommend_dept_name"));
			String InitialOperatorID=MyTools.ObjectToString(obj.get("initialOperatorID"));
			String InitialOperatorName=MyTools.ObjectToString(obj.get("initialOperatorName"));
			String rechargeCardCategory=MyTools.ObjectToString(obj.get("rechargeCardCategory"));
		//	String c_role_id=MyTools.ObjectToString(obj.get("c_role_id"));
			String point = MyTools.ObjectToString(obj.get("point"));
			String earnestMoney = MyTools.ObjectToString(obj.get("earnestMoney"));
			String discount_type = MyTools.ObjectToString(obj.get("discount_type"));
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.applyCardBackground(point,rechargeCardCategory,phone,family_phone,address,zipcode,recharge_amount, apply_way_code, 
					 auditor_id, auditor_name, desc,operater_id,operater_name,consume_recommend_id
						,consume_recommend_name,kids_parkID,babies,guardians,apply_type,consume_recommend_deptID,consume_recommend_dept_name,
						InitialOperatorID,InitialOperatorName,earnestMoney,discount_type,request);
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
		
		

		/*@RequestMapping(value = "/applyCardPayment")
		@ResponseBody
	    public Object applyCardPayment(String c_rechargecard_applicationID,String payment_amount,
				String card_number,String card_out_number,String contract_name,String begin_time,String end_time,String babies,String file,String callback,String OperatorID,String OperatorName,HttpServletResponse response) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
			response.setHeader("Access-Control-Allow-Origin", "http://oa.dibabo.cn");	
			
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.applyCardPayment(c_rechargecard_applicationID, payment_amount, card_number, card_out_number,contract_name,begin_time,end_time,file,OperatorID,OperatorName,0,null,null);
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }*/
		
		
		@RequestMapping(value = "/updateApplication")
		@ResponseBody
		public Object ApplicationUpdate(@RequestBody ApplicationUpdate application,BindingResult result,String callback,
				HttpServletRequest request) {
			
			
			Map<String, Object> m = new HashMap<String, Object>();
			Object obj=null;
			try {
			
				obj=rechargeApiService.updateapply(application, result,request);
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();
				return m;

			}

			if (ApplicationUtils.isNotNull(callback)) {
				return new JSONPObject(callback, obj);
			} else {
				return obj;
			}
		}
		

		@RequestMapping(value = "/getApplicationByID")
		@ResponseBody
	    public Object getApplicationByID(String applicationID,String callback,HttpServletResponse response) {
	    	//Map<String,Object> m  = rechargeApiService.applyCard(token, recharge_amount, name, identity_card, phone, apply_way_code);
	        
			response.setHeader("Access-Control-Allow-Origin", "http://oa.dibabo.cn");	
			
	    	Map<String, Object> m = new HashMap<String, Object>();
			try {

				m = rechargeApiService.getApplicationByID(applicationID);
				
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	    }
		
		
}


